<template>
  <div class="box board" v-loading="loading">
    <div class="flex">
      <el-button type="primary" plain>筛选样本</el-button>
      <span style="line-height: 32px; padding: 0 20px">分析样本量：100</span>
      <el-button type="primary">原始数据</el-button>
      <el-button type="warning" @click="startAnalysis">开始分析</el-button>
      <el-select
        v-if="prop.analysisMethod.value == 'Chi-Square'"
        style="width: 180px"
        class="ml-2"
        v-model="chiSquareSelect"
      >
        <el-option label="百分数(按列)" value="col" />
        <el-option label="百分数(按行)" value="row" />
      </el-select>
      <el-select
        v-if="prop.analysisMethod.value == 'gray'"
        style="width: 180px"
        class="ml-2"
        placeholder="量纲化方式"
        v-model="grayPandleType"
      >
        <el-option label="不量化(默认)" value="none" />
        <el-option label="初值化" value="init" />
        <el-option label="均值化" value="mean" />
      </el-select>
      <el-input-number
        v-if="prop.analysisMethod.value == 'gray'"
        style="width: 180px"
        class="ml-2"
        v-model="grayP"
        placeholder="分辨系数 默认0.5"
        controls-position="right"
        step="0.1"
        :min="0"
      />
      <el-input-number
        v-if="prop.analysisMethod.value == 'TTestSingle'"
        style="width: 180px"
        class="ml-2"
        v-model="popmean"
        placeholder="输入对比数字 默认0"
        controls-position="right"
      />
    </div>
    <el-row style="height: calc(100% - 32px)">
      <el-col :span="8" style="height: 100%">
        <div class="title">{{ currentData.dataName || '样本' }}</div>
        <div class="list-box box1">
          <draggable
            class="list-group"
            :list="currentData.metadata"
            group="group"
            @change="log($event, '1')"
            itemKey="name"
          >
            <template #item="{ element }">
              <div class="list-group-item">{{ element.comment }}</div>
            </template>
          </draggable>
        </div>
      </el-col>
      <el-col :span="2" style="height: 100%">
        <div class="change-over">
          <img src="../../../assets/image/changeOver.png" alt="" />
        </div>
      </el-col>
      <el-col :span="14" style="height: 100%">
        <el-row style="height: 100%">
          <el-col :span="24" :style="{ height: analysisMethod.x > 0 ? '200px' : '30px' }">
            <div class="title">选中样本</div>
            <div class="list-box box2" v-if="analysisMethod.x > 0">
              <div class="prompt" v-if="list2.length == 0">{{ analysisMethod.xLabel }}</div>
              <draggable class="list-group" :list="list2" group="group" @change="log($event, '2')" itemKey="name">
                <template #item="{ element }">
                  <div class="list-group-item">{{ element.comment }}</div>
                </template>
              </draggable>
            </div>
          </el-col>
          <el-col :span="24" :style="{ height: analysisMethod.x > 0 ? 'calc(100% - 180px)' : 'calc(100% - 10px)' }">
            <div class="list-box box3">
              <div class="prompt" v-if="list3.length == 0">{{ analysisMethod.yLabel }}</div>
              <draggable class="list-group" :list="list3" group="group" @change="log($event, '3')" itemKey="name">
                <template #item="{ element }">
                  <div class="list-group-item">{{ element.comment }}</div>
                </template>
              </draggable>
            </div>
          </el-col>
        </el-row>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import draggable from 'vuedraggable'
import { getNowData, dataAnalysis, getDataById } from '@/api/analysis'
import { onMounted } from 'vue-demi'
import { ElMessage } from 'element-plus'
const emit = defineEmits(['startAnalysis'])
const prop = defineProps({
  analysisMethod: {
    type: Object,
    default: () => ({})
  }
})
const list2 = ref([])
const list3 = ref([])
const currentData = ref({})
const chiSquareSelect = ref('col')
const grayPandleType = ref('')
const grayP = ref(null)
const popmean = ref(null)
const loading = ref(false)
const log = (e, type) => {
  if (type == 2) {
    if (list2.value.length > prop.analysisMethod.x) {
      list2.value.pop()
      currentData.value.metadata.push(e.added.element)
    }
  }
  if (type == 3) {
    if (list3.value.length > prop.analysisMethod.y) {
      list3.value.pop()
      currentData.value.metadata.push(e.added.element)
    }
  }
}
const startAnalysis = () => {
  let params
  if (prop.analysisMethod.value == 'Chi-Square') {
    params = {
      id: prop.analysisMethod._id,
      dataId: currentData.value._id, //数据的_id
      x: list2.value[0]?.id,
      y: list3.value.map((item) => item.id),
      percentage: chiSquareSelect.value,
      analysisAlgorithm: 'Chi-Square' //分析方法的名称
    }
  } else if (prop.analysisMethod.value == 'variance') {
    params = {
      id: prop.analysisMethod._id,
      dataId: currentData.value._id, //数据的_id
      x: list2.value[0]?.id,
      y: list3.value.map((item) => item.id),
      analysisAlgorithm: 'Anova' //分析方法的名称
    }
  } else if (prop.analysisMethod.value == 'frequency') {
    params = {
      id: prop.analysisMethod._id,
      dataId: currentData.value._id, //数据的_id
      x: list3.value.map((item) => item.id),
      analysisAlgorithm: 'Frequency' //分析方法的名称
    }
  } else if (prop.analysisMethod.value == 'gray') {
    params = {
      id: prop.analysisMethod._id,
      dataId: currentData.value._id, //数据的_id
      x: list2.value.map((item) => item.id),
      y: list3.value[0]?.id || '',
      analysisAlgorithm: 'GreyRelationAnalysis', //分析方法的名称
      handleType: grayPandleType.value == 'none' ? '' : grayPandleType.value,
      p: grayP.value == null ? '0.5' : grayP.value + ''
    }
  } else if (prop.analysisMethod.value == 'normalityTest') {
    params = {
      id: prop.analysisMethod._id,
      dataId: currentData.value._id, //数据的_id
      y: list2.value.map((item) => item.id),
      x: list3.value[0]?.id || '',
      analysisAlgorithm: 'NormalityTest' //分析方法的名称
    }
  } else if (prop.analysisMethod.value == 'TTestPaired') {
    if (list2.value.length != list3.value.length) {
      ElMessage.warning('配对1和配对2分析项需要完全对应!')
      return
    }
    params = {
      id: prop.analysisMethod._id,
      dataId: currentData.value._id, //数据的_id
      x: list2.value.map((item) => item.id),
      y: list3.value.map((item) => item.id),
      analysisAlgorithm: 'T-Test-Paired' //分析方法的名称
    }
  } else if (prop.analysisMethod.value == 'TTestSingle') {
    params = {
      id: prop.analysisMethod._id,
      dataId: currentData.value._id, //数据的_id
      x: list3.value.map((item) => item.id),
      analysisAlgorithm: 'T-Test-Single', //分析方法的名称
      popmean: popmean.value == null ? 0 : popmean.value
    }
  } else if (prop.analysisMethod.value == 'TTest') {
    params = {
      id: prop.analysisMethod._id,
      dataId: currentData.value._id, //数据的_id
      x: list2.value[0]?.id,
      y: list3.value.map((item) => item.id),
      analysisAlgorithm: 'T-Test' //分析方法的名称
    }
  }
  loading.value = true
  dataAnalysis(params)
    .then((res) => {
      console.log(res)

      emit('startAnalysis', { ...res.data, percentage: chiSquareSelect.value }, params)
    })
    .finally(() => {
      loading.value = false
    })
}
watch(
  () => prop.analysisMethod,
  () => {
    console.log(prop.analysisMethod)
    if (prop.analysisMethod?.param?.dataId) {
      getDataById(prop.analysisMethod?.param?.dataId).then((res) => {
        currentData.value = res.data
        list2.value = []
        list3.value = []
      })
    } else {
      getNowData().then((res) => {
        currentData.value = res.data
        list2.value = []
        list3.value = []
      })
    }
  },
  { deep: true, immediate: true }
)
onMounted(() => {
  console.log(prop.analysisMethod)
  getNowData().then((res) => {
    currentData.value = res.data
  })
})
</script>

<style lang="scss" scoped>
.box {
  background-color: #fff;
  box-shadow: 0 0 5px #ccc;
  border-radius: 5px;
}
.board {
  height: calc(100vh - 166px);
  padding: 10px 20px;
}
.title {
  position: relative;
  font-size: 20px;
  padding-left: 15px;
  line-height: 30px;
  color: #888;
  &::before {
    content: '';
    position: absolute;
    left: 0;
    top: 5px;
    height: 20px;
    width: 5px;
    background-color: #5473e8;
  }
}
.list-box {
  position: relative;
  background-color: #fafafa;
  border: 1px solid #ccc;
  box-shadow: 0 0 5px #ccc;
  margin-top: 10px;
  overflow: auto;
  .list-group {
    height: 100%;
    .list-group-item {
      padding: 0 20px;
      height: 40px;
      line-height: 40px;
      cursor: pointer;
      &:hover {
        background-color: #178aec;
        color: #fff;
      }
    }
  }
  .prompt {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: #888;
  }
}
.box1 {
  height: calc(100% - 40px);
}
.box2 {
  height: calc(100% - 50px);
}
.box3 {
  height: calc(100% - 30px);
}
.change-over {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  img {
    width: 60%;
  }
}
</style>
